From 9db3c2d8936077cbbdb6bc22936818cea0efb152 Mon Sep 17 00:00:00 2001 From: Carlos Garnacho Date: Thu, 17 Jan 2013 21:08:20 +0100 Subject: [PATCH] Add GtkGestureZoom This gesture interprets and reports relative scale differences when fed with events from two different GdkEventSequences. --- gtk/Makefile.am | 2 + gtk/gtk.h | 1 + gtk/gtkgesturezoom.c | 201 +++++++++++++++++++++++++++++++++++++++++++ gtk/gtkgesturezoom.h | 69 +++++++++++++++ 4 files changed, 273 insertions(+) create mode 100644 gtk/gtkgesturezoom.c create mode 100644 gtk/gtkgesturezoom.h diff --git a/gtk/Makefile.am b/gtk/Makefile.am index 878e747013..361b56436a 100644 --- a/gtk/Makefile.am +++ b/gtk/Makefile.am @@ -284,6 +284,7 @@ gtk_public_h_sources = \ gtkgesturelongpress.h \ gtkgesturerotate.h \ gtkgestureswipe.h \ + gtkgesturezoom.h \ gtkgrid.h \ gtkheaderbar.h \ gtkicontheme.h \ @@ -780,6 +781,7 @@ gtk_base_c_sources = \ gtkgesturelongpress.c \ gtkgesturerotate.c \ gtkgestureswipe.c \ + gtkgesturezoom.c \ gtkgrid.c \ gtkheaderbar.c \ gtkhsla.c \ diff --git a/gtk/gtk.h b/gtk/gtk.h index 8d17a7f291..7f716da173 100644 --- a/gtk/gtk.h +++ b/gtk/gtk.h @@ -110,6 +110,7 @@ #include #include #include +#include #include #include #include diff --git a/gtk/gtkgesturezoom.c b/gtk/gtkgesturezoom.c new file mode 100644 index 0000000000..f242d6d4eb --- /dev/null +++ b/gtk/gtkgesturezoom.c @@ -0,0 +1,201 @@ +/* GTK - The GIMP Toolkit + * Copyright (C) 2012, One Laptop Per Child. + * Copyright (C) 2014, Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + * + * Author(s): Carlos Garnacho + */ +#include "config.h" +#include +#include + +typedef struct _GtkGestureZoomPrivate GtkGestureZoomPrivate; + +enum { + SCALE_CHANGED, + LAST_SIGNAL +}; + +struct _GtkGestureZoomPrivate +{ + gdouble initial_distance; +}; + +static guint signals[LAST_SIGNAL] = { 0 }; + +G_DEFINE_TYPE_WITH_PRIVATE (GtkGestureZoom, gtk_gesture_zoom, GTK_TYPE_GESTURE) + +static void +gtk_gesture_zoom_init (GtkGestureZoom *gesture) +{ +} + +static GObject * +gtk_gesture_zoom_constructor (GType type, + guint n_construct_properties, + GObjectConstructParam *construct_properties) +{ + GObject *object; + + object = G_OBJECT_CLASS (gtk_gesture_zoom_parent_class)->constructor (type, + n_construct_properties, + construct_properties); + g_object_set (object, "n-points", 2, NULL); + + return object; +} + +static gboolean +_gtk_gesture_zoom_get_distance (GtkGestureZoom *zoom, + gdouble *distance) +{ + gdouble x1, y1, x2, y2; + GtkGesture *gesture; + GList *sequences; + gdouble dx, dy; + + gesture = GTK_GESTURE (zoom); + + if (!gtk_gesture_is_recognized (gesture)) + return FALSE; + + sequences = gtk_gesture_get_sequences (gesture); + g_assert (sequences && sequences->next); + + gtk_gesture_get_point (gesture, sequences->data, &x1, &y1); + gtk_gesture_get_point (gesture, sequences->next->data, &x2, &y2); + g_list_free (sequences); + + dx = x1 - x2; + dy = y1 - y2;; + *distance = sqrt ((dx * dx) + (dy * dy)); + + return TRUE; +} + +static gboolean +_gtk_gesture_zoom_check_emit (GtkGestureZoom *gesture) +{ + GtkGestureZoomPrivate *priv; + gdouble distance, zoom; + + if (!_gtk_gesture_zoom_get_distance (gesture, &distance)) + return FALSE; + + priv = gtk_gesture_zoom_get_instance_private (gesture); + + if (distance == 0 || priv->initial_distance == 0) + return FALSE; + + zoom = distance / priv->initial_distance; + g_signal_emit (gesture, signals[SCALE_CHANGED], 0, zoom); + + return TRUE; +} + +static void +gtk_gesture_zoom_begin (GtkGesture *gesture, + GdkEventSequence *sequence) +{ + GtkGestureZoom *zoom = GTK_GESTURE_ZOOM (gesture); + GtkGestureZoomPrivate *priv; + + priv = gtk_gesture_zoom_get_instance_private (zoom); + _gtk_gesture_zoom_get_distance (zoom, &priv->initial_distance); +} + +static void +gtk_gesture_zoom_update (GtkGesture *gesture, + GdkEventSequence *sequence) +{ + _gtk_gesture_zoom_check_emit (GTK_GESTURE_ZOOM (gesture)); +} + +static void +gtk_gesture_zoom_class_init (GtkGestureZoomClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + GtkGestureClass *gesture_class = GTK_GESTURE_CLASS (klass); + + object_class->constructor = gtk_gesture_zoom_constructor; + + gesture_class->begin = gtk_gesture_zoom_begin; + gesture_class->update = gtk_gesture_zoom_update; + + /** + * GtkGestureZoom::scale-changed: + * @controller: the object on which the signal is emitted + * @scale: Scale delta, taking the initial state as 1:1 + */ + signals[SCALE_CHANGED] = + g_signal_new ("scale-changed", + GTK_TYPE_GESTURE_ZOOM, + G_SIGNAL_RUN_FIRST, + G_STRUCT_OFFSET (GtkGestureZoomClass, scale_changed), + NULL, NULL, NULL, + G_TYPE_NONE, 1, G_TYPE_DOUBLE); +} + +/** + * gtk_gesture_zoom_new: + * @widget: a #GtkWidget + * + * Returns a newly created #GtkGesture that recognizes zoom + * in/out gestures (usually known as pinch/zoom) + * + * Returns: a newly created #GtkGestureZoom + * + * Since: 3.14 + **/ +GtkGesture * +gtk_gesture_zoom_new (GtkWidget *widget) +{ + return g_object_new (GTK_TYPE_GESTURE_ZOOM, + "widget", widget, + NULL); +} + +/** + * gtk_gesture_zoom_get_scale_delta: + * @gesture: a #GtkGestureZoom + * @scale: (out) (transfer none): zoom delta + * + * If @controller is active, this function returns %TRUE and fills + * in @scale with the zooming difference since the gesture was + * recognized (hence the starting point is considered 1:1). + * + * Returns: %TRUE if @controller is recognizing a zoom gesture + * + * Since: 3.14 + **/ +gboolean +gtk_gesture_zoom_get_scale_delta (GtkGestureZoom *gesture, + gdouble *scale) +{ + GtkGestureZoomPrivate *priv; + gdouble distance; + + g_return_val_if_fail (GTK_IS_GESTURE_ZOOM (gesture), FALSE); + + if (!_gtk_gesture_zoom_get_distance (gesture, &distance)) + return FALSE; + + priv = gtk_gesture_zoom_get_instance_private (gesture); + + if (scale) + *scale = distance / priv->initial_distance; + + return TRUE; +} diff --git a/gtk/gtkgesturezoom.h b/gtk/gtkgesturezoom.h new file mode 100644 index 0000000000..16a03b0370 --- /dev/null +++ b/gtk/gtkgesturezoom.h @@ -0,0 +1,69 @@ +/* GTK - The GIMP Toolkit + * Copyright (C) 2012, One Laptop Per Child. + * Copyright (C) 2014, Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + * + * Author(s): Carlos Garnacho + */ +#ifndef __GTK_GESTURE_ZOOM_H__ +#define __GTK_GESTURE_ZOOM_H__ + +#if !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION) +#error "Only can be included directly." +#endif + +#include +#include + +G_BEGIN_DECLS + +#define GTK_TYPE_GESTURE_ZOOM (gtk_gesture_zoom_get_type ()) +#define GTK_GESTURE_ZOOM(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GTK_TYPE_GESTURE_ZOOM, GtkGestureZoom)) +#define GTK_GESTURE_ZOOM_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), GTK_TYPE_GESTURE_ZOOM, GtkGestureZoomClass)) +#define GTK_IS_GESTURE_ZOOM(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GTK_TYPE_GESTURE_ZOOM)) +#define GTK_IS_GESTURE_ZOOM_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), GTK_TYPE_GESTURE_ZOOM)) +#define GTK_GESTURE_ZOOM_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GTK_TYPE_GESTURE_ZOOM, GtkGestureZoomClass)) + +typedef struct _GtkGestureZoom GtkGestureZoom; +typedef struct _GtkGestureZoomClass GtkGestureZoomClass; + +struct _GtkGestureZoom +{ + GtkGesture parent_instance; +}; + +struct _GtkGestureZoomClass +{ + GtkGestureClass parent_class; + + void (* scale_changed) (GtkGestureZoom *gesture, + gdouble scale); + /*< private >*/ + gpointer padding[10]; +}; + +GDK_AVAILABLE_IN_3_14 +GType gtk_gesture_zoom_get_type (void) G_GNUC_CONST; + +GDK_AVAILABLE_IN_3_14 +GtkGesture * gtk_gesture_zoom_new (GtkWidget *widget); + +GDK_AVAILABLE_IN_3_14 +gboolean gtk_gesture_zoom_get_scale_delta (GtkGestureZoom *gesture, + gdouble *scale); + +G_END_DECLS + +#endif /* __GTK_GESTURE_ZOOM_H__ */ -- 2.30.2